home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / bor_ti.exe / TI1032.ASC < prev    next >
Text File  |  1992-10-23  |  5KB  |  265 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.   PRODUCT  :  Borland C++                           NUMBER  :  1032
  9.   VERSION  :  3.x
  10.        OS  :  DOS
  11.      DATE  :  October 23, 1992                         PAGE  :  1/4
  12.  
  13.     TITLE  :  Example of Mouse Programming in Graphics Mode.
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.   //   This is a very simple drawing program which demonstrates
  21.   //   using the mouse by polling the mouse driver; all done with
  22.   //   interrupt 0x33 functions.
  23.  
  24.   #include <dos.h>
  25.   #include <stdio.h>
  26.   #include <conio.h>
  27.   #include <graphics.h>
  28.  
  29.   #define MOUSE       0x33
  30.   #define BUT1PRESSED  1
  31.   #define BUT2PRESSED  2
  32.   #define TRUE         1
  33.   #define FALSE        0
  34.  
  35.  
  36.  
  37.   void ActivMouse()
  38.   {
  39.        /* activate mouse */
  40.        _AX=32;
  41.        geninterrupt(MOUSE);
  42.   }
  43.  
  44.  
  45.  
  46.   int ResetMouse()
  47.   {
  48.        /* mouse reset */
  49.        _AX=0;
  50.        geninterrupt(MOUSE);
  51.        return(_AX);
  52.   }
  53.  
  54.  
  55.  
  56.   void ShowMouse()
  57.   {
  58.        /* turn on mouse cursor */
  59.        _AX=1;
  60.        geninterrupt(MOUSE);
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.   PRODUCT  :  Borland C++                           NUMBER  :  1032
  75.   VERSION  :  3.x
  76.        OS  :  DOS
  77.      DATE  :  October 23, 1992                         PAGE  :  2/4
  78.  
  79.     TITLE  :  Example of Mouse Programming in Graphics Mode.
  80.  
  81.  
  82.  
  83.  
  84.   }
  85.  
  86.  
  87.  
  88.   void HideMouse()
  89.   {
  90.        /* turn off mouse cursor */
  91.        _AX=2;
  92.        geninterrupt(MOUSE);
  93.   }
  94.  
  95.  
  96.  
  97.   void ReadMouse(int *v,int *h,int *but)
  98.   {
  99.        int temp;
  100.        _AX=3;
  101.        geninterrupt(MOUSE);
  102.  
  103.        // which buttons pressed: 1=left, 2=right, 3=both
  104.        temp=_BX;
  105.        *but=temp;
  106.  
  107.        // horizontal coordinates
  108.        *h =_CX;
  109.  
  110.        // vert coordinates
  111.        *v=_DX;
  112.   }
  113.  
  114.  
  115.  
  116.   int main( void )
  117.   {
  118.        int gdriver = EGA, gmode=EGAHI, errorcode;
  119.        int mouseX, mouseY, mouseButton=0, currentColor=1;
  120.        int oldMouseX=0, oldMouseY=0;
  121.        char button1Down,button2Down=FALSE;
  122.        if (!ResetMouse())
  123.        {
  124.             printf("No Mouse Driver!");
  125.             return(1);
  126.        }
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.   PRODUCT  :  Borland C++                           NUMBER  :  1032
  141.   VERSION  :  3.x
  142.        OS  :  DOS
  143.      DATE  :  October 23, 1992                         PAGE  :  3/4
  144.  
  145.     TITLE  :  Example of Mouse Programming in Graphics Mode.
  146.  
  147.  
  148.  
  149.  
  150.        printf(   "Instructions:\nHold down left button to draw,\n"
  151.                  "Click right button to change color,\n"
  152.                  "When done drawing, hit key.\n"
  153.                  "Press any key to continue\n" );
  154.        getch();
  155.        initgraph(&gdriver,&gmode, "");
  156.        if (graphresult()!=grOk)
  157.        {
  158.             printf( "Graphics Init Error -- EGAVGA.BGI not in "
  159.                     "current directory?" );
  160.             return(1);
  161.        }
  162.  
  163.        setcolor(WHITE);
  164.        outtextxy(1,1,"COLOR");
  165.        ActivMouse();
  166.        ShowMouse();
  167.        while (!kbhit())
  168.        {
  169.             ReadMouse(&mouseY,&mouseX,&mouseButton);
  170.             if (mouseButton&BUT1PRESSED)
  171.             {
  172.                  if (mouseX!=oldMouseX || mouseY!=oldMouseY)
  173.                  {
  174.                       // hide mouse cursor before drawing anything!
  175.                       HideMouse();
  176.                       if (button1Down)
  177.                            line(mouseX,mouseY,oldMouseX,oldMouseY);
  178.                       oldMouseX=mouseX; oldMouseY=mouseY;
  179.                       ShowMouse();
  180.                  }
  181.                  button1Down=TRUE;
  182.             }
  183.             else
  184.                  button1Down=FALSE;
  185.             if (mouseButton&BUT2PRESSED)
  186.             {
  187.                  button2Down=TRUE;
  188.             }
  189.             else if (button2Down)
  190.             {
  191.                  // If button 2 was down and is now up, it was
  192.                  // clicked!
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.   PRODUCT  :  Borland C++                           NUMBER  :  1032
  207.   VERSION  :  3.x
  208.        OS  :  DOS
  209.      DATE  :  October 23, 1992                         PAGE  :  4/4
  210.  
  211.     TITLE  :  Example of Mouse Programming in Graphics Mode.
  212.  
  213.  
  214.  
  215.  
  216.                  button2Down=FALSE;
  217.  
  218.                  // so change the color
  219.                  ++currentColor;
  220.  
  221.                  if (currentColor==16)
  222.                       currentColor=1;
  223.  
  224.                  setcolor(currentColor);
  225.                  HideMouse();
  226.                  outtextxy(1,1,"COLOR");
  227.                  ShowMouse();
  228.             }
  229.        }
  230.        closegraph();
  231.        return( 0 );
  232.   }
  233.  
  234.  
  235.  
  236.   DISCLAIMER: You have the right to use this technical information
  237.   subject to the terms of the No-Nonsense License Statement that
  238.   you received with the Borland product to which this information
  239.   pertains.
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.